//
//  CMDBNormalTitleViewController.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/15.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBPublishCommentsViewController: NormalTitleViewController {
    
    var tid : String = ""
    
    var commentBtn : UIButton!
    
    var commentCount : Int = 0
    
    var textView : CMDBCommentView!
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        textView.becomeFirstResponder()
        
    }

    override func buildTitleView() {
        super.buildTitleView()
        
        let button                    = UIButton(frame : CGRect(x: CMDB_Screen_Width - 140, y: 0, width: 100, height: 64))
        
        commentBtn                    = button
        
        button.center                 = CGPoint(x: CMDB_Screen_Width - 20, y: self.titleView!.middleY + 10)
        
        button.setTitle("发送", forState: .Normal)
        
        button.titleLabel?.font       = CMDB_16_FONT
        
        button.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        
        button.enabled                = false
        
        self.titleView?.addSubview(button)
        
        button.addTarget(self, action: #selector(sendItemClick), forControlEvents: .TouchUpInside)
        
        
    }

    override func buildContentView() {
        
        super.buildContentView()
        
        textView          = CMDBCommentView(frame: (contentView?.bounds)!, textContainer: nil)
        
        textView.delegate = self
        
        contentView?.addSubview(textView)
        
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

}

extension CMDBPublishCommentsViewController{
    
    // MARK:- 事件监听函数

        @objc private func sendItemClick() {
            
            let commentParam     = CMDBCommentParam()
            
            commentParam.tid     = tid
            
            commentParam.content = textView.text
            
            do{
                try CMDBCommentManager.getInstance.comment(commentParam, handler: { (string) in
                    
                    
                    self.dataBlock!("comment",1,nil)
                    
                    self.popSelf()
                    
                    }, error: { (rescode) in
                        
                    }, failure: {
                        
                })
            }catch{
                
            }

        }
        
        @objc private func keyboardWillChangeFrame(note : NSNotification) {
            
        }
    
}

// MARK:- UITextView的代理方法
extension CMDBPublishCommentsViewController : UITextViewDelegate {
    func textViewDidChange(textView: UITextView) {
        
        self.textView.placeHolderLabel.hidden = textView.hasText()
        
        commentBtn.enabled                    = textView.hasText()
        
        commentBtn.setTitleColor(commentBtn.enabled ? UIColor.orangeColor() : UIColor.lightGrayColor(), forState: .Normal)
    }
    
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        textView.resignFirstResponder()
    }
}


extension CMDBPublishCommentsViewController{
    
    private func setupNotifications() {
        // 监听键盘的弹出
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CMDBPublishCommentsViewController.keyboardWillChangeFrame(_:)), name: UIKeyboardWillChangeFrameNotification, object: nil)
        
    }
}